package servlet;

import pojo.News;
import pojo.NewsPage;
import service.NewsService;
import service.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GetAllNews extends HttpServlet {
    private NewsService newsService = new NewsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        Map<String,Object> params = new HashMap<>();
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        Integer pid = (page == null)? 1 : Integer.parseInt(page);
        Integer num = (size == null)? 5 : Integer.parseInt(size);
        String title = req.getParameter("name");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        params.put("beginTime",beginTime);
        params.put("endTime",endTime);
        params.put("title",title);
        params.put("page",pid);
        params.put("size",num);
        NewsPage newsPage = newsService.getAllNews(params);
        req.setAttribute("newsPage", newsPage);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
